Skip to content

Type Annotations for RestrictedPython#317

Draft
zedzhen wants to merge 44 commits into
zopefoundation:masterfrom
zedzhen:typing
Draft

Type Annotations for RestrictedPython#317
zedzhen wants to merge 44 commits into
zopefoundation:masterfrom
zedzhen:typing

Conversation

@zedzhen
Copy link
Copy Markdown
Contributor

@zedzhen zedzhen commented Jun 2, 2026

  • I signed and returned the Zope Contributor Agreement, and received and accepted an invitation to join a team in the zopefoundation GitHub organization.
  • I verified there aren't any other open pull requests for the same change.
  • I followed the guidelines in Developer guidelines.
  • I successfully ran code quality checks on my changes locally.
  • I successfully ran tests on my changes locally.
  • If needed, I added new tests for my changes.
  • If needed, I added documentation for my changes.
  • I included a change log entry in my commits.

This PR is based on #303. But PR #303 is not active.
Closes #303

loechel and others added 30 commits October 18, 2025 11:31
# Conflicts:
#	.meta.toml
#	CHANGES.rst
#	setup.py
#	pyproject.toml
#	src/RestrictedPython/transformer.py
# Conflicts:
#	.meta.toml
Copy link
Copy Markdown
Member

@icemac icemac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of adding type annotations but I am missing checks that the type annotations are correct (mypy or ty preferably run via pre-commit.) Otherwise we cannot say that the current type annotations are correct and will stay correct with future changes.

Comment thread src/RestrictedPython/compile.py Outdated
Comment on lines +5 to +16
from ast import Expression
from ast import Interactive
from ast import Module
from ast import NodeTransformer
from collections.abc import Mapping
from collections.abc import Sequence
from os import PathLike
from types import CodeType
from typing import Any
from typing import Literal
from typing import NamedTuple
from typing import TypeAlias
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like these from imports, below in the code it is unclear where a name comes from, please stick to normal imports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@icemac Is it allowed to use import typing as t?

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class typing support (PEP 561) across RestrictedPython by introducing type annotations in key modules and marking the distribution as typed.

Changes:

  • Add/expand type annotations in the compiler and AST transformer code paths.
  • Introduce py.typed and advertise typing support via package metadata.
  • Minor typing-related cleanup in utilities and changelog messaging.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/RestrictedPython/Utilities.py Adds typing for reorder() and imports Iterable.
src/RestrictedPython/transformer.py Adds extensive annotations to transformer helpers and visit_* methods.
src/RestrictedPython/py.typed Adds PEP 561 marker file for typed package distribution.
src/RestrictedPython/compile.py Adds annotations, CompileResult as NamedTuple, and typed signatures for compile helpers.
pyproject.toml Declares Typing :: Typed and adds a typecheck extra.
CHANGES.rst Adds changelog entry for type-annotation work.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/RestrictedPython/compile.py Outdated
Comment thread pyproject.toml
Comment thread src/RestrictedPython/transformer.py Outdated
Comment thread src/RestrictedPython/transformer.py Outdated
Comment thread src/RestrictedPython/compile.py Outdated
Comment thread src/RestrictedPython/compile.py Outdated
policy=RestrictingNodeTransformer):
source: str | ReadableBuffer | Module | Expression | Interactive,
filename: str | ReadableBuffer | PathLike[Any] = '<string>',
mode: Literal["exec", "eval", "single"] = "exec",
Comment thread src/RestrictedPython/compile.py Outdated
policy=RestrictingNodeTransformer):
source: str | ReadableBuffer | Module | Expression | Interactive,
filename: str | ReadableBuffer | PathLike[Any] = '<unknown>',
mode: str = 'exec',
@zedzhen zedzhen marked this pull request as draft June 3, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants